<?php

class My_Html_MagicTable
{
    const OPTION_SHOW_TABLE_HEADERS = 'showTableHeaders';
    const OPTION_USE_COLUMN_AS_CHECK = 'useColumnAsCheck';
    const OPTION_USE_COLUMN_AS_RADIO = 'useColumnAsRadio';
    const OPTION_RENDER_AS_LIST = 'renderAsList';
    const OPTION_ITEMS_PER_PAGE = 'itemsPerPage';

    /**
     * @var string
     */
    protected $id;

    /**
     * @var string
     */
    protected $url;

    /**
     * @var bool 
     */
    protected $showHeaders = false;

    /**
     * @var string 
     */
    protected $columnAsCheck;

    /**
     * @var string 
     */
    protected $columnAsRadio;

    /**
     * @var string
     */
    protected $title;

    /**
     * @var boolean
     */
    protected $showPaginator;

    /**
     * @var Zend_View
     */
    protected $view;

    /**
     * @var boolean
     */
    protected $renderAsList = false;
    
    /**
     * @var integer
     */
    protected $intemsPerPage = 20;
    /**
     * @var string
     */
    protected $renderScript = 'default.phtml';

    protected function __construct($id)
    {
        $this->id = $id;
        $this->view = new Zend_View();
        $this->view->addScriptPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'magic-table');
        $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
    }

    /**
     * @param string $id
     * @return My_Html_MagicTable
     */
    public static function factory($id = null)
    {
        if (is_null($id))
            $id = uniqid();
        return new self($id);
    }

    /**
     * @param string $url
     * @return My_Html_MagicTable 
     */
    public function loadDataFrom($url)
    {
        $this->url = $url;
        return $this;
    }

    /**
     * @return string 
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * @param string $id
     * @return My_Html_MagicTable 
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return string 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return boolean 
     */
    public function getShowHeaders()
    {
        return $this->showHeaders;
    }

    /**
     * @param boolean $showHeaders
     * @return My_Html_MagicTable 
     */
    public function setShowHeaders($showHeaders = true)
    {
        $this->showHeaders = $showHeaders;
        return $this;
    }

    /**
     * @return string 
     */
    public function getColumnAsCheck()
    {
        return $this->columnAsCheck;
    }

    /**
     * @param string $columnAsCheck
     * @return My_Html_MagicTable 
     */
    public function setColumnAsCheck($columnAsCheck)
    {
        $this->columnAsCheck = $columnAsCheck;
        return $this;
    }

    /**
     * @return string 
     */
    public function getColumnAsRadio()
    {
        return $this->columnAsRadio;
    }

    /**
     * @param string $columnAsRadio
     * @return My_Html_MagicTable 
     */
    public function setColumnAsRadio($columnAsRadio)
    {
        $this->columnAsRadio = $columnAsRadio;
        return $this;
    }

    /**
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param string $title
     * @return My_Html_MagicTable 
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }

    /**
     * @return boolean 
     */
    public function getShowPaginator()
    {
        return $this->showPaginator;
    }

    /**
     * @param boolean $showPaginator
     * @return My_Html_MagicTable 
     */
    public function setShowPaginator($showPaginator)
    {
        $this->showPaginator = $showPaginator;
        return $this;
    }

    /**
     * @param boolean $value
     * @return My_Html_MagicTable
     */
    public function setRenderAsList($value = true)
    {
        $this->renderAsList = $value;
        return $this;
    }    

    /**
     * @return boolean
     */
    public function getRenderAsList()
    {
        return $this->renderAsList;
    }    
    
    /**
     * @return integer
     */
    public function getIntemsPerPage()
    {
        return $this->intemsPerPage;
    }

    /**
     * @param integer $intemsPerPage
     * @return My_Html_MagicTable
     */
    public function setIntemsPerPage($intemsPerPage)
    {
        $this->intemsPerPage = $intemsPerPage;
        return $this;
    }

        
    /**
     * @param booelan $jsonEncoded
     * @return mixed (array | json) 
     */
    public function getOptions($jsonEncoded = false)
    {
        $options = array();
        if (!is_null($this->getShowHeaders()))
            $options[self::OPTION_SHOW_TABLE_HEADERS] = $this->getShowHeaders();
        if (!is_null($this->getColumnAsCheck()))
            $options[self::OPTION_USE_COLUMN_AS_CHECK] = $this->getColumnAsCheck();
        if (!is_null($this->getColumnAsRadio()))
            $options[self::OPTION_USE_COLUMN_AS_RADIO] = $this->getColumnAsRadio();
        if (!is_null($this->getRenderAsList()))
            $options[self::OPTION_RENDER_AS_LIST] = $this->getRenderAsList();
        if (true == $jsonEncoded)
        if (!is_null($this->getIntemsPerPage()))
            $options[self::OPTION_ITEMS_PER_PAGE] = $this->getIntemsPerPage ();
        if (true == $jsonEncoded)
            $options = Zend_Json::encode($options);
        return $options;
    }

    /**
     * @return Zend_View
     */
    public function getView()
    {
        return $this->view;
    }

    /**
     * @param Zend_View $view
     * @return My_Html_Panel
     */
    public function setView($view)
    {
        $this->view = $view;
        return $this;
    }

    /**
     * @return string
     */
    public function getRenderScript()
    {
        $renderScript = $this->renderScript;
        return $renderScript;
    }

    /**
     * @param string $renderScript
     * @return My_Html_Panel
     */
    public function setRenderScript($renderScript)
    {
        $this->renderScript = $renderScript;
        return $this;
    }

    /**
     * @return string
     */
    public function render()
    {
        $view = $this->getView();
        $view->table = $this;
        return $view->render($this->getRenderScript());
    }

    /**
     * @return string
     */
    public function __toString()
    {
        try
        {
            $htmlCode = $this->render();
        }
        catch (Exception $e)
        {
            $htmlCode = sprintf('Error rendering magic-table %s: %s (%s).', $this->getId(), $this->getTitle(), $e->getMessage());
        }
        return $htmlCode;
    }

}